/**
 * 
 */
package com.aspire.android.heliar.raw.mms;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.aspire.android.heliar.raw.Raw;
import com.aspire.android.heliar.raw.RawAttachment;

/**
 * @author panglei
 * 
 */
public class MMS extends Raw {

	public static final String MMS_SUBJECT_SEP = "\n-----------------------------------";
	public static final String MMS_PARAGRAPH_SEP = "\n\n";
	public static final String MMS_ATTACHMENT_TEMPLETE = "ATT#%s#";

	private int id;
	private String number;
	private String subject;
	private int threadId;
	private int date;
	private String address;

	private MMSSmil smil;

	private List<MMSFrame> frames = new LinkedList<MMSFrame>();

	public void addMMSFrame(MMSFrame frame) {
		frames.add(frame);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public MMSSmil getSmil() {
		return smil;
	}

	public void setSmil(MMSSmil smil) {
		this.smil = smil;
	}

	public List<MMSFrame> getFrames() {
		return frames;
	}

	public void setFrames(List<MMSFrame> frames) {
		this.frames = frames;
	}

	public Date getDate() {
		return new Date(date * 1000L);
	}

	public void setDate(int date) {
		this.date = date;
	}

	public int getThreadId() {
		return threadId;
	}

	public void setThreadId(int threadId) {
		this.threadId = threadId;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.aspire.android.heliar.raw.RAW#getFullContent()
	 */
	@Override
	public String getFullContent() {

		if (fullContent != null) {
			return fullContent;
		}

		fullContent = "";

		for (MMSFrame frame : this.getFrames()) {
			if (frame instanceof MMSTextFrame) {
				MMSTextFrame textFrame = (MMSTextFrame) frame;
				fullContent += textFrame.getText() + MMS_PARAGRAPH_SEP;
			} else if (frame instanceof MMSImageFrame) {
				MMSImageFrame imageFrame = (MMSImageFrame) frame;
				fullContent += String.format(MMS_ATTACHMENT_TEMPLETE,
						imageFrame.getCid()) + MMS_PARAGRAPH_SEP;

			} else if (frame instanceof MMSSoundFrame) {
				MMSSoundFrame soundFrame = (MMSSoundFrame) frame;
				fullContent += String.format(MMS_ATTACHMENT_TEMPLETE,
						soundFrame.getCid()) + MMS_PARAGRAPH_SEP;
			}
		}
		return fullContent;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.aspire.android.heliar.raw.RAW#getAttachmentFrameMap()
	 */
	@Override
	public Map<String, RawAttachment> getAttachmentFrameMap() {
		if (attachmentFrames != null) {
			return attachmentFrames;
		}

		attachmentFrames = new HashMap<String, RawAttachment>();
		for (MMSFrame frame : this.getFrames()) {
			if (frame instanceof MMSTextFrame) {
			} else if (frame instanceof MMSImageFrame) {
				MMSImageFrame imageFrame = (MMSImageFrame) frame;
				attachmentFrames.put(imageFrame.getCid(), imageFrame);
			} else if (frame instanceof MMSSoundFrame) {
				MMSSoundFrame soundFrame = (MMSSoundFrame) frame;
				attachmentFrames.put(soundFrame.getCid(), soundFrame);
			}
		}

		return attachmentFrames;
	}
}
